package com.example.rydwlib

import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.TextView
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.baselib.activity.BaseViewModelActivity
import com.example.baselib.constants.ARConstants
import com.example.rydwlib.adapter.RydwAllNickAdapter
import com.example.rydwlib.bean.RyNickBean
import kotlinx.android.synthetic.main.activity_rydw_all_nick.*
import java.lang.Exception

@Route(path = ARConstants.APP_RYDW_NICK)
class RydwAllNickActivity : BaseViewModelActivity<RydwAllViewModel>() {
    private lateinit var rydwAllNickAdapter: RydwAllNickAdapter
    override fun providerVMClass(): Class<RydwAllViewModel>? {
        return RydwAllViewModel::class.java
    }

    override fun layoutResId(): Int =R.layout.activity_rydw_all_nick

    override fun initView() {
        setToolbarMsg("人员定位")
        setToolbarColor(R.color.colorDBlue, R.color.colorWhite)
        setToolbarMenu(true, R.id.menu_safe)
        rydwAllNickAdapter =RydwAllNickAdapter(R.layout.rydw_all_nick_item)
        val empty = LayoutInflater.from(this).inflate(R.layout.empty_item, null, false)
        rydwAllNickAdapter.emptyView=empty
        rydw_all_nick_rcy.adapter=rydwAllNickAdapter
//        rydwAllNickAdapter.bindToRecyclerView(rydw_all_nick_rcy)
        rydw_all_nick_rcy.layoutManager =  LinearLayoutManager(this, LinearLayoutManager.VERTICAL,false)
        rydwAllNickAdapter.setOnItemClickListener { adapter, view, position ->
            val ryNickBean = adapter.getItem(position) as RyNickBean
            val intent=Intent(this,RydwMainActivity::class.java)
            intent.putExtra("unitname",ryNickBean.minename)
            intent.putExtra("unitcode",ryNickBean.minecode)
            startActivity(intent)
        }
        ry_all_nick_srl.setColorSchemeColors(resources.getColor(R.color.colorDBlue))
        ry_all_nick_srl.setOnRefreshListener {
            viewModel.isLoad=false
            viewModel.loadRydwNickData() }

    }

    override fun requestFinally(it: Int?) {
        super.requestFinally(it)
        ry_all_nick_srl.isRefreshing=false
    }

    override fun requestError(it: Exception?) {

        ry_all_nick_empty.visibility= View.VISIBLE
        if (it?.message=="暂无数据"){
            ry_all_nick_empty.findViewById<TextView>(R.id.err_txt).text=it?.message
            ry_all_nick_empty.findViewById<TextView>(R.id.err_txt).textSize=18f
            ry_all_nick_empty.findViewById<TextView>(R.id.err_msg).visibility=View.INVISIBLE
//            ry_main_empty.findViewById<ImageView>(R.id.err_img).visibility=View.INVISIBLE
        }else{
            ry_all_nick_empty.findViewById<TextView>(R.id.err_msg).text=it?.message
            super.requestError(it)
        }
        ry_all_nick_empty.findViewById<Button>(R.id.err_rest_btn).setOnClickListener {  viewModel.loadRydwNickData() }
    }
    override fun initData() {
        viewModel.getRydwNickData().observe(this, Observer {
            rydwAllNickAdapter.setNewData(it)
            ry_all_nick_empty.visibility= View.INVISIBLE
        })
    }
}
